From 558e75347e63e5a4e23ce1be8a6baddeae488d9c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 9 Jul 2014 13:55:00 -0700 Subject: [PATCH] Fix --release not compiling upstream deps with -O3 This touches up the filtering logic to ensure that upstream dependencies as well as local dependencies are built with optimizations when `cargo build --release` is used. --- src/cargo/ops/cargo_compile.rs | 2 +- src/cargo/ops/cargo_rustc.rs | 10 +-- tests/support/mod.rs | 1 + tests/test_cargo_compile.rs | 119 ++++++++++++++++++++++++++++++++- tests/test_cargo_test.rs | 26 +++++++ 5 files changed, 151 insertions(+), 7 deletions(-) diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index cadc97a9c..e8f13b3a1 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -78,7 +78,7 @@ pub fn compile(manifest_path: &Path, options: CompileOptions) -> CargoResult<()> }).collect::>(); let mut config = try!(Config::new(shell, update, jobs)); - try!(ops::compile_targets(targets.as_slice(), &package, + try!(ops::compile_targets(env.as_slice(), targets.as_slice(), &package, &PackageSet::new(packages.as_slice()), &mut config)); Ok(()) diff --git a/src/cargo/ops/cargo_rustc.rs b/src/cargo/ops/cargo_rustc.rs index 3d3b5660e..14cf3d963 100644 --- a/src/cargo/ops/cargo_rustc.rs +++ b/src/cargo/ops/cargo_rustc.rs @@ -41,7 +41,8 @@ fn uniq_target_dest<'a>(targets: &[&'a Target]) -> Option<&'a str> { curr.unwrap() } -pub fn compile_targets<'a>(targets: &[&Target], pkg: &Package, deps: &PackageSet, +pub fn compile_targets<'a>(env: &str, targets: &[&Target], pkg: &Package, + deps: &PackageSet, config: &'a mut Config<'a>) -> CargoResult<()> { if targets.is_empty() { @@ -84,11 +85,10 @@ pub fn compile_targets<'a>(targets: &[&Target], pkg: &Package, deps: &PackageSet for dep in deps.iter() { // Only compile lib targets for dependencies let targets = dep.get_targets().iter().filter(|target| { - target.is_lib() && target.get_profile().is_compile() + target.is_lib() && target.get_profile().get_env() == env }).collect::>(); - jobs.push((dep, - try!(compile(targets.as_slice(), dep, &mut cx)))); + jobs.push((dep, try!(compile(targets.as_slice(), dep, &mut cx)))); } cx.primary = true; @@ -285,7 +285,7 @@ fn build_base_args(into: &mut Args, into.push(format!("metadata={}", m.metadata)); into.push("-C".to_str()); - into.push(format!("extra-filename={}", m.extra_filename)); + into.push(format!("extra-filename=-{}", m.extra_filename)); } None => {} } diff --git a/tests/support/mod.rs b/tests/support/mod.rs index 9dfcd696b..72e643b6e 100644 --- a/tests/support/mod.rs +++ b/tests/support/mod.rs @@ -414,6 +414,7 @@ pub fn basic_bin_manifest(name: &str) -> String { "#, name, name) } +pub static RUNNING: &'static str = " Running"; pub static COMPILING: &'static str = " Compiling"; pub static FRESH: &'static str = " Fresh"; pub static UPDATING: &'static str = " Updating"; diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index 0cc9214ee..10f82fbb2 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -1,9 +1,10 @@ use std::io::fs; use std::os; use std::path; +use std::str; use support::{ResultTest, project, execs, main_file, escape_path, basic_bin_manifest}; -use support::COMPILING; +use support::{COMPILING, RUNNING}; use hamcrest::{assert_that, existing_file}; use cargo; use cargo::util::{process, realpath}; @@ -954,3 +955,119 @@ test!(missing_lib_and_bin { .with_stderr("either a [[lib]] or [[bin]] section \ must be present\n")); }) + +test!(verbose_build { + let mut p = project("foo"); + p = p + .file("Cargo.toml", r#" + [package] + + name = "test" + version = "0.0.0" + authors = [] + "#) + .file("src/lib.rs", ""); + let output = p.cargo_process("cargo-build").arg("-v") + .exec_with_output().assert(); + let out = str::from_utf8(output.output.as_slice()).assert(); + let hash = out.slice_from(out.find_str("extra-filename=").unwrap() + 15); + let hash = hash.slice_to(17); + assert_eq!(out, format!("\ +{} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib -g \ + -C metadata=test:-:0.0.0:-:file:{dir} \ + -C extra-filename={hash} \ + --out-dir {dir}{sep}target \ + -L {dir}{sep}target \ + -L {dir}{sep}target{sep}deps` +{} test v0.0.0 (file:{dir})\n", + RUNNING, COMPILING, + dir = p.root().display(), + sep = path::SEP, + hash = hash).as_slice()); +}) + +test!(verbose_release_build { + let mut p = project("foo"); + p = p + .file("Cargo.toml", r#" + [package] + + name = "test" + version = "0.0.0" + authors = [] + "#) + .file("src/lib.rs", ""); + let output = p.cargo_process("cargo-build").arg("-v").arg("--release") + .exec_with_output().assert(); + let out = str::from_utf8(output.output.as_slice()).assert(); + let hash = out.slice_from(out.find_str("extra-filename=").unwrap() + 15); + let hash = hash.slice_to(17); + assert_eq!(out, format!("\ +{} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib \ + --opt-level 3 \ + -C metadata=test:-:0.0.0:-:file:{dir} \ + -C extra-filename={hash} \ + --out-dir {dir}{sep}target{sep}release \ + -L {dir}{sep}target{sep}release \ + -L {dir}{sep}target{sep}release{sep}deps` +{} test v0.0.0 (file:{dir})\n", + RUNNING, COMPILING, + dir = p.root().display(), + sep = path::SEP, + hash = hash).as_slice()); +}) + +test!(verbose_release_build_deps { + let mut p = project("foo"); + p = p + .file("Cargo.toml", r#" + [package] + + name = "test" + version = "0.0.0" + authors = [] + + [dependencies.foo] + path = "foo" + "#) + .file("src/lib.rs", "") + .file("foo/Cargo.toml", r#" + [package] + + name = "foo" + version = "0.0.0" + authors = [] + "#) + .file("foo/src/lib.rs", ""); + let output = p.cargo_process("cargo-build").arg("-v").arg("--release") + .exec_with_output().assert(); + let out = str::from_utf8(output.output.as_slice()).assert(); + let pos1 = out.find_str("extra-filename=").unwrap(); + let hash1 = out.slice_from(pos1 + 15).slice_to(17); + let pos2 = out.slice_from(pos1 + 10).find_str("extra-filename=").unwrap(); + let hash2 = out.slice_from(pos1 + 10 + pos2 + 15).slice_to(17); + assert_eq!(out, format!("\ +{running} `rustc {dir}{sep}foo{sep}src{sep}lib.rs --crate-name foo \ + --crate-type lib \ + --opt-level 3 \ + -C metadata=foo:-:0.0.0:-:file:{dir} \ + -C extra-filename={hash1} \ + --out-dir {dir}{sep}target{sep}release{sep}deps \ + -L {dir}{sep}target{sep}release{sep}deps \ + -L {dir}{sep}target{sep}release{sep}deps` +{running} `rustc {dir}{sep}src{sep}lib.rs --crate-name test --crate-type lib \ + --opt-level 3 \ + -C metadata=test:-:0.0.0:-:file:{dir} \ + -C extra-filename={hash2} \ + --out-dir {dir}{sep}target{sep}release \ + -L {dir}{sep}target{sep}release \ + -L {dir}{sep}target{sep}release{sep}deps` +{compiling} foo v0.0.0 (file:{dir}) +{compiling} test v0.0.0 (file:{dir})\n", + running = RUNNING, + compiling = COMPILING, + dir = p.root().display(), + sep = path::SEP, + hash1 = hash1, + hash2 = hash2).as_slice()); +}) diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 62116160b..07a9f0e4f 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -55,3 +55,29 @@ test!(test_with_lib_dep { assert_that(p.cargo_process("cargo-test"), execs().with_status(0)); }) + +test!(test_with_deep_lib_dep { + let p = project("foo") + .file("Cargo.toml", r#" + [project] + name = "bar" + version = "0.0.1" + authors = [] + + [dependencies.foo] + path = "foo" + "#) + .file("src/lib.rs", " + extern crate foo; + pub fn bar() {} + ") + .file("Cargo.toml", r#" + [project] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", ""); + + assert_that(p.cargo_process("cargo-test"), execs().with_status(0)); +}) -- 2.30.2